﻿namespace EFS.RC.CCAdmin.Surveys.Answer
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		#region Locals
		/// modal dialog host
		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		/// question id
		private System.Int32 quesionId = 0;

		/// score values
		public static System.String[] ScoreValues = new System.String[] { "N/A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
		#endregion

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32 quesionId,
			System.Int32? answerID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.SurveyAnswer_GetCompleted += UtilitiesService_SurveyAnswer_GetCompleted;

			//set new answer response listener
			UtilitiesService.SurveyAnswer_CreateCompleted += UtilitiesService_SurveyAnswer_CreateCompleted;

			//set edit response listener
			UtilitiesService.SurveyAnswer_UpdateCompleted += UtilitiesService_SurveyAnswer_UpdateCompleted;

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.AnswerID = answerID;
			this.quesionId = quesionId;
		}
		#endregion

		#region Properties
		/// Property AnswerID: Get, Set
		public System.Int32? AnswerID { get; set;}

		/// Property IsValid: Get
		private System.Boolean IsValid
		{
			get
			{
				return EFS.Common.Verify.String(AnswerName) && (comboBoxScore.SelectedValue != null) && Priority.HasValue;
			}
		}

		/// Property IsValid: Get
		private System.String AnswerName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxAnswerName.Text);
			}
		}

		/// Property IsValid: Priority
		private System.Int32? Priority
		{
			get
			{
				System.Int32 retValue = 0;
				if (System.Int32.TryParse(textBoxPriority.Text, out retValue))
				{
					return retValue;
				}

				// none
				return null;
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			//score values
			comboBoxScore.ItemsSource = ScoreValues;

			// if edit
			if (AnswerID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

				// call ws
				UtilitiesService.SurveyAnswer_GetAsync(AnswerID.Value);
			}
		}
		#endregion

		#region WS Callbacks
		/// GetAnswerDetailsCompleted
		private void UtilitiesService_SurveyAnswer_GetCompleted(object sender, WebProxy.SurveyAnswer_GetCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Answer answer =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Answer>(e.Result.ToString());

				//bind/fill form
				textBoxAnswerName.Text = answer.Text;
				if (-1 == answer.Score)
				{
					comboBoxScore.SelectedItem = "N/A";
				}
				else
				{
					comboBoxScore.SelectedItem = answer.Score.ToString();
				}
				textBoxPriority.Text = answer.Priority.HasValue ? answer.Priority.Value.ToString() : "";
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve answer. " + ex.Message);
			}
		}

		/// Create answer completed
		private void UtilitiesService_SurveyAnswer_CreateCompleted(object sender, WebProxy.SurveyAnswer_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create answer. " + ex.Message);
			}
		}

		/// Update answer completed
		private void UtilitiesService_SurveyAnswer_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update answer. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (!IsValid)
			{
				return;
			}

			// create
			if (!AnswerID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while creating answer");

				//ws call
				UtilitiesService.SurveyAnswer_CreateAsync(quesionId, AnswerName, (System.String)comboBoxScore.SelectedValue, Priority.Value);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while updating answer");

				//ws call
				UtilitiesService.SurveyAnswer_UpdateAsync(AnswerID.Value, quesionId, AnswerName, (System.String)comboBoxScore.SelectedValue, Priority.Value);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}